<?php
namespace ApiWF\Controller;

use Common\Controller\ApibaseController;

class UploadController extends ApibaseController
{
    // 文件上传
    protected function baseUploader($subFolderName)
    {

        $upload_setting = sp_get_upload_setting();

        $filetypes = array(
            'image' => array('title' => 'Image files', 'extensions' => $upload_setting['image']['extensions']),
            'video' => array('title' => 'Video files', 'extensions' => $upload_setting['video']['extensions']),
            'audio' => array('title' => 'Audio files', 'extensions' => $upload_setting['audio']['extensions']),
            'file' => array('title' => 'Custom files', 'extensions' => $upload_setting['file']['extensions'])
        );

        $image_extensions = explode(',', $upload_setting['image']['extensions']);

        $all_allowed_exts = array();
        foreach ($filetypes as $mfiletype) {
            array_push($all_allowed_exts, $mfiletype['extensions']);
        }
        $all_allowed_exts = implode(',', $all_allowed_exts);
        $all_allowed_exts = explode(',', $all_allowed_exts);
        $all_allowed_exts = array_unique($all_allowed_exts);

        $file_extension = sp_get_file_extension($_FILES['file']['name']);
        $upload_max_filesize = $upload_setting['upload_max_filesize'][$file_extension];
        $upload_max_filesize = empty($upload_max_filesize) ? 2097152 : $upload_max_filesize;//默认2M
        // echo "test1>>";
        // print_r($_FILES);
        // file_put_contents('test.txt',var_export($_FILES,true)."\n",FILE_APPEND);
        // file_put_contents('test.txt',var_export($_POST,true)."\n",FILE_APPEND);
        // exit;
        $savepath = $subFolderName . '/' . date('Ymd') . '/';
        //上传处理类
        $config = array(
            'rootPath' => './' . C("UPLOADPATH"),
            'savePath' => $savepath,
            'maxSize' => $upload_max_filesize,
            'saveName' => array('uniqid', ''),
            'exts' => $all_allowed_exts,
            'autoSub' => false,
        );
        $upload = new \Think\Upload($config);//
        $info = $upload->upload();
        //开始上传
        if ($info) {
            //上传成功
            $oriName = $_FILES['file']['name'];
            // echo "<<test2>>";
            // print_r($_FILES);


            //写入附件数据库信息
            $first = array_shift($info);
            if (!empty($first['url'])) {
                $url = $first['url'];
                $storage_setting = sp_get_cmf_settings('storage');
                $qiniu_setting = $storage_setting['Qiniu']['setting'];
                $url = preg_replace('/^https/', $qiniu_setting['protocol'], $url);
                $url = preg_replace('/^http/', $qiniu_setting['protocol'], $url);

                $preview_url = $url;

                if (in_array($file_extension, $image_extensions)) {
                    if (C('FILE_UPLOAD_TYPE') == 'Qiniu' && $qiniu_setting['enable_picture_protect']) {
                        $preview_url = $url . $qiniu_setting['style_separator'] . $qiniu_setting['styles']['thumbnail300x300'];
                        $url = $url . $qiniu_setting['style_separator'] . $qiniu_setting['styles']['watermark'];
                    }
                } else {
                    $preview_url = '';
                    $url = sp_get_file_download_url($first['savepath'] . $first['savename'], 3600 * 24 * 365 * 50);//过期时间设置为50年
                }

            } else {
                $url = C("TMPL_PARSE_STRING.__UPLOAD__") . $savepath . $first['savename'];
                $preview_url = $url;
            }
            $filepath = $savepath . $first['savename'];
            // echo $filepath;
            // exit;
            //更新用户头像
            $uid = $this->parseTokenUID();
            $data['avatar'] = $filepath;
            D('Users')->where(['id' => $uid])->save($data);

            $filepath = qiniu_mirror_image_url($filepath);
            $this->jsonReturn(array('preview_url' => $preview_url, 'filepath' => $filepath, 'url' => $url, 'name' => $oriName,));

        } else {
            $this->error($upload->getError());
        }
    }


    public function uploadUserAvatar()
    {
        //test git hook
        $this->baseUploader('avatar');
    }


}
